﻿using Abp.Dependency;
using Abp.Extensions;
using Mt.Site.Core.WorkFlows.Enum;
using Mt.Site.Core.WorkFlows.Process;
using Mt.Site.Core.WorkFlows.Process.Entities;
using System.Threading.Tasks;

namespace Mt.Site.Core.WorkFlows.Diy.Actions.Impl
{
    /// <summary>
    /// 办结操作
    /// </summary>
    public class FinishedAction : ActionBase
    {
        public override async Task<string> DisplayName(IWorkFlowManager manager)
        {
            var hasNext = await manager.CanGoNextAsync();
            if (hasNext)
            {
                return UniqueName;
            }
            else
            {
                if (DiyStaticFunction.IsApprovalFlow(manager.WorkFlow.Definition.CustomData))
                {
                    //将提交拆分为 同意和 不同意
                    return "通过审批";
                }
                else
                {
                    return "提交";
                }
            }
        }

        public override string UniqueName => WorkFlowConst.OperationName.FinishedActionName;

        /// <summary>
        /// 是否显示该操作按钮（auditing.ActionStatus == ActionState.Todo,!auditing.HasNext）
        /// </summary>
        /// <param name="manager"></param>
        /// <returns></returns>
        public override async Task<bool> IsShowAsync(IWorkFlowManager manager)
        {
            var actionState = await manager.CheckActionStateAsync();
            var isBranchLine = await manager.IsBranchLineAsync();
            var canOperate = manager.CanOperate(UniqueName);
            var hasNext = await manager.CanGoNextAsync();
            var hasOtherOperator = (actionState == ActionState.Todo ? await manager.IsMultiplePeopleAsync() : false);

            //当前是待办，并且没有后续步骤
            if (actionState == ActionState.Todo
                && !isBranchLine
                && !hasOtherOperator
                && (canOperate || !hasNext))
            {
                using (var guidang = manager.IocResolver.ResolveAsDisposable<GuiDangAction>())
                {
                    if (await guidang.Object.IsShowAsync(manager))
                    {
                        return false;
                    }
                }

                return true;
            }
            return false;
        }

        /// <summary>
        /// 操作处理,拷贝文件时可能抛出异常
        /// </summary>
        /// <param name="auditing"></param>
        /// <param name="data"></param>
        public override async Task OperateAsync(IWorkFlowManager manager, object data)
        {
            var isShow = await IsShowAsync(manager);
            if (isShow)
            {
                await manager.WorkFlowPluginFunction.BeforeFinishedAsync(manager);
                var args = data as ActionArgs;

                //信息稿件状态切换为办结,文件状态改为发布状态
                var entity = manager.CurrentPaper.Paper;
                entity.ChangePaperState(PaperState.Finished);

                if (DiyStaticFunction.IsApprovalFlow(manager.WorkFlow.Definition.CustomData))
                {
                    entity.ChangeApprovalResult(ApprovalResult.Pass);  //同意

                    if (args.ReplyMsg.IsNullOrEmpty())
                    {
                        args.ReplyMsg = "通过";
                    }
                }

                //状态数据切换为已处理
                var current = await manager.GetCurrentProcessAsync();

                await manager.HasSolved(args.ReplyMsg, WorkFlowProcess.DefaultReplyMsg, MoveMode.End);

                //写入日志
                await manager.AddLogAsync("办结", manager.UserId, null, current?.Id);

                await manager.WorkFlowPluginFunction.AfterFinishedAsync(manager);
            }
        }

        public override bool NeedReply(IWorkFlowManager manager)
        {
            return false;
        }

        public override bool NeedCheckState => false;
    }
}
